package team.first.dataimport;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import org.neo4j.driver.Session;
import team.first.util.DriverUtil;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first.dataimport
 * @Date: 2021/12/3 16:44
 * @Description: comment作为用户和电影的关系，内容等作为关系的属性
 */
public class CommentImport {
    public static void commentImport() {
        Session session = DriverUtil.getNewSession();
        try {
            List<Entity> commentList = Db.use().query("select * from comment");

            String addComment = "MATCH (m:Movie{id:$movieId})" +
                                "MATCH (u:User{id:$userId})" +
                                "CREATE (u) - [:COMMENT{" +
                                                "id:$id," +
                                                "createdAt:$createdAt, " +
                                                "rating:$rating, " +
                                                "content:$content" +
                                                "}] -> (m)";
            commentList.forEach(comment -> {
                Map<String, Object> params = new HashMap<>();
                params.put("id", comment.getInt("id"));
                params.put("movieId", comment.getInt("movie_id"));
                params.put("userId", comment.getInt("user_id"));
                params.put("createdAt", comment.getStr("created_at"));
                params.put("rating", comment.getInt("rating"));
                params.put("content", comment.getStr("content"));
                session.run(addComment, params);
            });
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DriverUtil.close(session);
        }
    }
}
